﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using System.Threading;

public partial class SystemManage_ClientLook : BasePage
{
    ISyClientHandler clientManage = new SyClientHandler();
    ISystemHandler systemManage = new SystemHandler();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!strPermission.Contains("1034"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }
            FillDll();
            ClientDataBind();
            dataBind();
        }
    }

    private void dataBind()
    {

        SqlParameter[] parmsL = new SqlParameter[1];
        parmsL[0] = new SqlParameter("@ClientID", Request["ClientID"]);
        string sqlL = "select * from Sy_ClientLinkMan where ClientID=@ClientID";
        DataTable dtLink = clientManage.GetClientLinkManList(sqlL, parmsL);

        if (dtLink != null && dtLink.Rows.Count>0) 
        {
            divEntrust.Visible = true;

            DataView view = dtLink.DefaultView;
            // view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
            gvClientList.DataSource = view;
            gvClientList.DataBind();
            if (dtLink != null && dtLink.Rows.Count > 0)
            {
                lblTotal.Text = dtLink.Rows.Count.ToString();
            }
            else
            {
                lblTotal.Text = "0";
            }

        }
        else
        {
            divEntrust.Visible = false;
        }
    }

    private void ClientDataBind()
    {
        Sy_ClientQuery query = clientManage.GetClient(int.Parse(Request["ClientID"]));
        if (query != null)
        {
            txtNameCNS.Text = query.CNShortName;
            txtNameENS.Text = query.ENGShortName;
            txtNameCN.Text = query.CNName;
            txtNameEN.Text = query.ENGName;
            txtAddrCN.Text = query.CNAddress;
            txtAddrEN.Text = query.ENGAddress;
            //txtInvoiteTitle.Text = query.Title;
            txtEmail.Text = query.Email;
            txtWebsite.Text = query.WebSite;
            txtPShiper.Text = query.PShiper;
            //txtLocalBP.Text = query.LocalBP;
            txtCompanyBP.Text = systemManage.GetUserName(query.CompanyBP);
           // txtPlace.Text = query.Place;
            txtRemark.Text = query.Remark;
            txtNameHKS.Text = query.HKShortName;
            txtNameHK.Text = query.HKName;
            txtAddrHK.Text = query.HKAddress;
            //txtInvoiteTitleEN.Text = query.ENGTitle;
           // txtInvoiteTitleHK.Text = query.HKTitle;
            txtPlaceSel.Text = GetPlace(query.PlaceID.ToString());

            txtMVendorCode.Text = query.MVendorCode;
            txtHVendorCode.Text = query.HVendorCode;
            txtToYantian.Text = query.ToYantian != 0 ? query.ToYantian.ToString() : string.Empty;
            txtToShekou.Text = query.ToShekou != 0 ? query.ToShekou.ToString() : string.Empty;
            txtGpsWebsite.Text = query.GPSWebsite;
            if (query.Status == 1)
            {
                radYes.Checked = true;
                radNo.Checked = false;
            }
            else
            {
                radYes.Checked = false;
                radNo.Checked = true;
            }

            DataTable dtType = clientManage.GetClientTypeByID(query.PKID);
            if (dtType != null && dtType.Rows.Count > 0)
            {

                List<string> lstPermissions = new List<string>();
                string[] str = new string[dtType.Rows.Count];
                for (int i = 0; i < dtType.Rows.Count; i++)
                {
                    str[i] = dtType.Rows[i]["SyCodeID"].ToString();
                }
                lstPermissions.AddRange(str);

                foreach (ListItem item in chkClientList.Items)
                {
                    if (lstPermissions.Contains(item.Value))
                    {
                        item.Selected = true;
                    }
                }
            }
        }
    }

    public string GetPlace(object id)
    {
        return systemManage.GetPlace(id);
    }

    private void FillDll()
    {
      
        DataTable dtT = clientManage.GetClientTypeList();
        if (dtT != null && dtT.Rows.Count > 0)
        {
            chkClientList.DataSource = dtT;
            if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
            {
                chkClientList.DataTextField = "ENGName";
            }
            else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
            {
                chkClientList.DataTextField = "HKName";
            }
            else
            {
                chkClientList.DataTextField = "Name";
            }
            chkClientList.DataValueField = "PKID";
            chkClientList.DataBind();
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("ClientList.aspx");

    }

    protected void gvClientList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvClientList.PageIndex = e.NewPageIndex;
        dataBind();
    }

    protected void lbtnGo_Click(object sender, EventArgs e)
    {
        TextBox txtPages = ((TextBox)gvClientList.BottomPagerRow.FindControl("txtPages"));
        try
        {
            int i = Convert.ToInt32(txtPages.Text) - 1;
            if (i >= 0 && i < gvClientList.PageCount)
            {
                this.gvClientList.PageIndex = i;
                //数据绑定
                dataBind();
            }
            else
            {
                Jscript.AlertInAjax(Resources.Messages.WrongPageNumInput, UpdatePanel1);
                return;
            }
        }
        catch
        {
            Jscript.AlertInAjax(Resources.Messages.WrongPageNumInput, UpdatePanel1);
        }
    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvClientList.PageSize = int.Parse(ddlPages.SelectedValue);
        dataBind();
    }
      
}
    
